<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index</title>
    <!-- Required Stylesheets -->
    <link href="/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
<div class="layui-btn-container common">
    <button type="button" class="layui-btn" id="btnAdd">增加</button>
    <button type="button" class="layui-btn" id="btnEdit">编辑</button>
    <button type="button" class="layui-btn" id="btnDel">删除</button>
</div>
<div class="container">
    <div class="layui-col-md2">
        <div id="invcTree" class="demo-tree demo-tree-box"></div>
    </div>
    <div class="layui-col-md10">
        <form class="layui-form layui-form-pane" action="" method="post">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">存货分类编码</label>
                    <div class="layui-input-inline">
                        <input type="hidden" id="iID" name="iID" value="0" />
                        <input type="text" id="cInvCCode" name="cInvCCode" required lay-verify="required"
                               placeholder="请输入存货分类编码" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">存货分类名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="cInvCName" name="cInvCName" required lay-verify="required"
                               placeholder="请输入存货分类名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">上级存货分类</label>
                    <div class="layui-input-inline">
                        <input type="hidden" id="iParentID" name="iParentID" value="0" />
                        <input type="text" id="cParentName" name="cParentName" placeholder="请输入存货分类名称"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- Required Javascript -->
<script src="/js/jquery-3.3.1.min.js"></script>
<script src="/layui/layui.all.js"></script>
<script src="/layui/extend/tableSelect.js"></script>
<script type="text/javascript">
    function buildDomTree() {
        var data = [];

        $.ajax({
            type: "GET",
            async: false,
            url: "/inventoryClass/getTree",
            success: function (msg) {
                // console.log(msg);
                data = msg.data;
            }
        });
        // console.log(data);
        return data;
    }

    //layui.use('tree', function () {
    var tree = layui.tree
        , layer = layui.layer
        , util = layui.util
        //模拟数据
        , data1 = buildDomTree();

    //常规用法
    tree.render({
        elem: '#invcTree' //默认是点击节点可进行收缩
        , id: 'invcTree'
        , data: data1
        , click: function (obj) {
            //console.log(obj.data); //得到当前点击的节点数据
            //console.log(obj.state); //得到当前节点的展开状态：open、close、normal
            //console.log(obj.elem); //得到当前节点元素
            //console.log(obj.data.children); //当前节点下是否有子节点
            $.ajax({
                type: "GET",
                async: false,
                data: "id=" + obj.data.id,
                url: "/inventoryClass/getByID",
                success: function (msg) {
                    console.log(JSON.parse(msg));
                    var data=JSON.parse(msg).data;
                    console.log(data.iID);
                    $('#iID').val(data.iID);
                    $('#cInvCCode').val(data.cInvCCode);
                    $('#cInvCName').val(data.cInvCName);
                    $('#iParentID').val(data.iParentID);
                    $('#cParentName').val(data.cParentName);
                }
            });
        }
    });

    //});
</script>

<script>
    var form = layui.form;
    form.render();

    //弹出一个iframe层
    $('#btnAdd').on('click', function () {
        layer.open({
            type: 2,
            id: "InventoryClassCreate",
            title: '新增存货分类',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '520px'],
            content: '/inventoryClass/create',
            end: function () {
                //do something
                //console.log('end回调');
                //layer.close(index); //如果设定了yes回调，需进行手工关闭
                //
                tree.reload('invcTree', {
                    //新的参数
                    data: buildDomTree()
                });
            }
        });
    });

    $('#btnEdit').on('click', function () {
        if ($('#iID').val() == 0) {
            //配置一个透明的询问框
            layer.msg('请先选择要修改的存货分类。', {
                time: 5000, //20s后自动关闭
                btn: ['确认']
            });
            return;
        }
        layer.open({
            type: 2,
            id: "InventoryClassEdit",
            title: '修改存货分类',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '520px'],
            content: '/inventoryClass/update?id=' + $('#iID').val(),
            end: function () {
                //do something
                //console.log('end回调');
                //layer.close(index); //如果设定了yes回调，需进行手工关闭
                //
                tree.reload('invcTree', {
                    //新的参数
                    data: buildDomTree()
                });
            }
        });
    });

    $('#btnDel').on('click', function () {
        if ($('#iID').val() == 0) {
            //配置一个透明的询问框
            layer.msg('请先选择要删除的存货分类。', {
                time: 5000, //20s后自动关闭
                btn: ['确认']
            });
            return;
        }

        console.log($('#iID').val());
        layer.confirm('是否要删除当前存货分类？', {
            btn: ['确认', '取消'] //可以无限个按钮
        }, function (index, layero) {
            console.log(layero);
            console.log("{id:"+$('#iID').val()+"}");
            //alert("id=" + $('#iID').val());
            //按钮【确认】的回调
            $.ajax({
                type: "POST",
                data: "id=" + $('#iID').val(),
                dataTypeString: "json",
                async: false,
                url: "/inventoryClass/delete",
                success: function (msg) {
                    console.log(msg);
                    if (msg.code == "0") {
                        tree.reload('invcTree', {
                            //新的参数
                            data: buildDomTree()
                        });
                        $('#iID').val("");
                        $('#cInvCCode').val("");
                        $('#cInvCName').val("");
                        $('#iParentID').val("");
                        $('#cParentName').val("");
                    }
                    else {
                        layer.msg('错误号：' + msg.code + ' 错误信息：' + msg.msg, { icon: 2 });
                    }
                }
            });
            layer.close(index);
        }, function (index, layro) {
            //按钮【取消】的回调
        });
    });
</script>
</body>
</html>
